const path = require("path");
const fs = require("fs");
const YAML = require("yaml")

//读取文件
function read_data(file, data) {
    const dir = path.join(file, `${data}.json`);
    let enroll = fs.readFileSync(dir, "utf8", (err, data) => {
        if (err) {
            console.log(err);
            return "error";
        }
        return data;
    });
    //将字符串数据转变成数组格式
    enroll = JSON.parse(enroll);
    return enroll;
}

function write_data(file, data, enroll) {
    const path = require("path");
    const fs = require("fs");
    const dir = path.join(file, `${data}.json`);
    const new_ARR = JSON.stringify(enroll, "", "\t");
    fs.writeFileSync(dir, new_ARR, "utf8", (err) => {
        console.log("写入成功", err);
    });
    return;
}

function getPath_yaml(id, name) {
    const file = read_data(path.resolve(), "file");
    const bot_file = path.join(file.file_data, "yunzai", `Yunzai-${id}`, "config", "config");
    return path.join(bot_file, `${name}.yaml`);
}
function read_yaml(id, name) {
    return YAML.parse(fs.readFileSync(getPath_yaml(id, name), 'utf8'));
}
function write_yaml(id, name, data) {
    return fs.writeFileSync(getPath_yaml(id, name), YAML.stringify(data), 'utf8');
}